package com.elitel.frame.base.service.impl;

import com.elitel.frame.base.component.RedisCacheDataBase;
import com.elitel.frame.base.component.exportprojectdata.DataBaseConnInfo;
import com.elitel.frame.base.service.BaseFieldconfigService;
import com.elitel.frame.main.dao.BaseFieldconfigMapper;
import com.elitel.frame.main.dao.CfgFieldMapper;
import com.elitel.frame.main.dao.ext.ExtBaseFieldaddonsMapper;
import com.elitel.frame.main.dao.ext.ExtBaseFieldconfigMapper;
import com.elitel.frame.main.dao.ext.ExtCfgFieldMapper;
import com.elitel.frame.main.entity.BaseFieldconfig;
import com.elitel.frame.main.entity.CfgField;
import com.elitel.frame.main.entity.DataSourceType;
import com.elitel.frame.main.entity.ext.BaseFieldconfigExt;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guoyanfei
 * @Description 元数据配置-其他配置服务实现类
 */
@Service
public class BaseFieldconfigServiceImpl implements BaseFieldconfigService {

    @Autowired
    private ExtBaseFieldconfigMapper extBaseFieldconfigMapper;
    @Autowired
    private BaseFieldconfigMapper baseFieldconfigMapper;
    @Autowired
    private ExtCfgFieldMapper extCfgFieldMapper;
    @Autowired
    private CfgFieldMapper cfgFieldMapper;
    @Autowired
    private RedisCacheDataBase redisCacheDataBase;
    @Autowired
    private ExtBaseFieldaddonsMapper extBaseFieldaddonsMapper;
    @Autowired
    private DataBaseConnInfo dataBaseConnInfo;

    @Override
    public PageResponseVo<BaseFieldconfigExt> getListByPage(PageRequestVo<BaseFieldconfigExt> pr) {
        String databaseType = dataBaseConnInfo.dataBaseType;
        List<BaseFieldconfigExt> list = new ArrayList<BaseFieldconfigExt>();

        switch (databaseType) {
            case "oracle":
            case DataSourceType.DM:
                // TODO 适配达梦,待测试,接口对应的功能未启用
                pr.StartPage();
                list = extBaseFieldconfigMapper.getListBypage(pr);
                break;
            case "mysql":
            case "sqlite":
            case "h2":
                pr.setPageNo(pr.getPageNo() - 1);
                list = extBaseFieldconfigMapper.getListBypageMysql(pr);
                break;
            case DataSourceType.POSTGRES:
                pr.setPageNo(pr.getPageNo() - 1);
                list = extBaseFieldconfigMapper.getListBypagePG(pr);
                break;
        }

        Integer total = extBaseFieldconfigMapper.getListCount(pr);
        PageResponseVo<BaseFieldconfigExt> responseVo = new PageResponseVo<BaseFieldconfigExt>();
        responseVo.setRows(list);
        responseVo.setTotal(total);

        return responseVo;
    }

    @Override
    public Integer insertInfo(BaseFieldconfig baseFieldconfig) {
        Integer result = 0;
        result = baseFieldconfigMapper.insertSelective(baseFieldconfig);
        if (result > 0) {
            //更新缓存数据
            CfgField cf = cfgFieldMapper.selectByPrimaryKey(baseFieldconfig.getFieldid());
            if (cf != null) {
                redisCacheDataBase.refreshFieldConfigRedisCache(cf.getDtConfigId());
            }
        }
        return result;
    }

    @Override
    public Integer updateInfo(BaseFieldconfig baseFieldconfig) {
        Integer result = 0;
        result = baseFieldconfigMapper.updateByPrimaryKeySelective(baseFieldconfig);
        if (result > 0) {
            //更新缓存数据
            CfgField cf = cfgFieldMapper.selectByPrimaryKey(baseFieldconfig.getFieldid());
            if (cf != null) {
                redisCacheDataBase.refreshFieldConfigRedisCache(cf.getDtConfigId());
            }
        }
        return result;
    }

    @Override
    public void batchDelInfo(String guids) {
        String[] strguid = guids.split(",");
        for (String str : strguid) {
            BaseFieldconfig baseFieldconfig = baseFieldconfigMapper.selectByPrimaryKey(str);

            //删除关联表
            baseFieldconfigMapper.deleteByPrimaryKey(str);

            //更新缓存数据
            if (baseFieldconfig != null) {
                CfgField cf = cfgFieldMapper.selectByPrimaryKey(baseFieldconfig.getFieldid());
                if (cf != null) {
                    redisCacheDataBase.refreshFieldConfigRedisCache(cf.getDtConfigId());
                }

                //删除已配置的显示列
                extBaseFieldaddonsMapper.deleteBaseFieldaddonsBydtconfigandaltername(
                        baseFieldconfig.getDtConfigId(), baseFieldconfig.getAltername());
            }

        }

    }


    @Override
    public List<CfgField> selectFieldBydbkeyAndtable(String dbkey, String tablename) {
        return extCfgFieldMapper.selectFieldBydbkeyAndtable(dbkey, tablename);
    }

    @Override
    public List<BaseFieldconfigExt> getAllFieldConfig(String dtconfigid) {
        List<BaseFieldconfigExt> baseFieldconfigExts = new ArrayList<BaseFieldconfigExt>();
        baseFieldconfigExts = extBaseFieldconfigMapper.getAllFieldConfig(dtconfigid);
        return baseFieldconfigExts;
    }

    @Override
    public BaseFieldconfig getFieldConfigByFieldId(String fieldId) {
        BaseFieldconfig baseFieldconfig = extBaseFieldconfigMapper.getFieldconfigByFieldid(fieldId);
        if (baseFieldconfig != null) {
            if (baseFieldconfig.getForeignType().equals("2")) {
                return baseFieldconfig;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public String getAlterNameBydtConfigId(String dtConfigId) {
        String result = "";
        List<String> alternames = extBaseFieldconfigMapper.getFieldConfigAltername(dtConfigId);
        if (alternames != null && alternames.size() > 0) {
            result = alternames.get(0);
        }
        return result;
    }

    @Override
    public String getAlternameBydtConfigIdAndLinkField(String dtConfigId, String linkField) {
        return extBaseFieldconfigMapper.getAlternameBydtConfigIdAndLinkField(dtConfigId, linkField);
    }

    @Override
    public BaseFieldconfig getFieldEnumeration(String fieldId, String dtConfigId) {
        return extBaseFieldconfigMapper.getFieldEnumeration(fieldId, dtConfigId);
    }


    /**
     * TODO: 根据 dtConfigId 批量删除
     *
     * @param dtConfigId
     * @return
     */
    @Override
    public Integer batchDelete(String dtConfigId) {
        return extBaseFieldconfigMapper.batchDelete(dtConfigId);
    }


}
